// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function () { // 通用预处理器

	},
	async add(params = {}) {
		let dbJQL = uniCloud.databaseForJQL({clientInfo: this.getClientInfo()});
		return await dbJQL.collection('xxm-activity-banner').add(params);
	},
	
	async list() {
		let dbJQL = uniCloud.databaseForJQL({clientInfo: this.getClientInfo()});
		
		let bannerTemp = dbJQL.collection('xxm-activity-banner').orderBy('sort', 'desc').getTemp();
		let userTemp = dbJQL.collection('uni-id-users').field("_id, nickname").getTemp();
		
		let res = await dbJQL.collection(bannerTemp, userTemp)
			.field("appid, checked, createTime, picurl, sort, target, url, user_id.nickname as nickname, user_id._id as user_id")
			.get();
		let data = res.data.map((item => ({...item, nickname: item.nickname[0], user_id: item.user_id[0]})));
		return {...res, data};
	},
	
	async update(params = {}) {
		let {_id, ...data} = params;
		let dbJQL = uniCloud.databaseForJQL({clientInfo: this.getClientInfo()});
		return await dbJQL.collection('xxm-activity-banner').doc(_id).update({
			...data,
			createTime: Date.now()
		});
	}
}

